﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Commons.Collections;
 
using NVelocity.App;
using CARMSBusiness;

namespace CARMS
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            TaskCollection tasks = (TaskCollection)ConfigurationManager.GetSection("tasks");  //获取web.config 中 tasks的 节 ,并取得配置项
            foreach (TaskConfiguration config in tasks)  //如果有多个键值对,则分别读取这些键值对
            {
                config.CreateTask();
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies.Get("UserSetting");
            // Check if cookie exists in the current request.
            if (cookie == null)
            {
                Session["CurrentCulture"] = CultureNames.Chinese;
                // Create cookie.
                cookie = new HttpCookie("UserSetting");
                // Set value of cookie to current date time.
                cookie.Value = DateTime.Now.ToString();
                // Set cookie to expire in 10 minutes.
                cookie.Expires = DateTime.Now.AddDays(30d);
                cookie.Values.Add("culture", Session["CurrentCulture"].ToString());
                // Insert the cookie in the current HttpResponse.
                Response.Cookies.Add(cookie);

            }
            else
            {
                Session["CurrentCulture"] = cookie.Values["culture"].ToString();
            }
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            var userData = (UserData)Session["CurrentLoginUser"];
            LogBusiness.UpdateLog(Session.SessionID, DateTime.Now, userData.LoginName);

            HttpCookie cookie = Request.Cookies.Get("UserSetting");
            if (cookie != null)
            {
                cookie["culture"] = Session["CurrentCulture"].ToString();
                Response.Cookies.Add(cookie);
            }
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }


    public class CultureNames
    {
        public static string Chinese
        {
            get
            {
                return "zh-CN";
            }
        }

        public static string ChineseGb //繁体
        {
            get
            {
                return "zh-TW";
            }
        }

        public static string English
        {
            get
            {
                return "en-US";
            }
        }
    }
}